package com.example.lib_ui_base

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.os.Handler
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams
import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.IdRes
import kotlinx.coroutines.delay
import java.util.Timer
import java.util.TimerTask
import java.util.zip.Inflater


class WjbAlertView {
    private var mContext:Context
    private var mView:View
    private var mWindowManager:WindowManager

    companion object{
        val timer = Timer()
        var isStart = false
    }

    constructor(mContext: Context, resId:Int){
        this.mContext  = mContext
        //解析器
        val layoutInflater = LayoutInflater.from(mContext)
        //解析
        this.mView = layoutInflater.inflate(resId,null)
        //获取windowManager
        mWindowManager = mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    }

    constructor(mContext: Context,mView:View){
        this.mContext  = mContext
        this.mView = mView
        //获取windowManager
        mWindowManager = mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    }

    //显示
    fun show() {
        //这里需要进行不同的设置

        val lp = WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT
        )
        lp.gravity = Gravity.CENTER_HORIZONTAL or Gravity.TOP

        //添加视图
        mWindowManager.addView(mView, lp)




        timer.schedule(object : TimerTask() {
                override fun run() {
                    dismiss()
                }
            }, 800)


    }

        //隐藏
        fun dismiss() {
            mWindowManager.removeView(mView)
        }
}